Runs the script action once for each place of target, from the first to the last, handing it that place's value. action is the NAME of one of your scripts and it must take exactly one argument. It is the same as writing a loop over target that calls the script with target[i].
array_foreach(target, action)| Name | Type |
|---|---|
target | array |
action | script |
Nothing.
Runs a script once for each place in target, an array, from the first place to the last. Each time, the script is handed the value in that place.
It does the same job as this loop, written once:
for (var i = 0; i < array_length(positions); i++) {
spawn_coin(positions[i]);
}array_foreach(positions, spawn_coin);The second thing you give is the name of one of your scripts: a file in the project's scripts folder with a function named like the file. Write just the name, with no brackets after it. Here is a script that makes a coin at a place across the screen:
// scripts/spawn_coin.gbml
function spawn_coin(x: int) {
instance_create(x, 20, obj_coin);
}The script has to follow three rules, and each one is checked before the game runs:
x: int. An array of whole numbers fits a parameter that wants decimals, but an array of decimals does not fit one that wants whole numbers. A script that is only used here can leave the type out: it takes the kind of the values of the array. Write it when you want it checked.You do not have to make a script file for a short job. Write the function right where the name would go:
var sum = 0;
array_foreach(nums, function (x) { sum += x; });It is the script, only without a name. Its one parameter takes the kind of the values of the array, so you do not write the kind (you may, and then it has to fit). It can read and change the variables around it, such as a local you made on the line before, and a return inside it leaves only the function, never the event or script around it. Use a script file instead when the same job is needed in more than one place.
| You do this | What happens |
|---|---|
give a function written out in the middle of the line, like function (x) { ... } | that is fine: it is the script, only without a name (see "A function written out in place" above). It must take exactly one parameter, and it can use and change the variables around it |
give the name of a variable, or of a built-in function like instance_destroy | refused when checked. It has to be one of your scripts |
| give a script with no parameters, or with two or more | refused when checked |
| give a script whose parameter is a different kind from the values, like a text parameter for an array of numbers | refused when checked, and the message says which kinds do not match |
| give it a list or a grid | refused when checked, and the message names the function for that kind, like list_foreach |
| use it where a value is needed | refused when checked. It gives nothing back |
A script that takes an array as a parameter works on the caller's own array (see Arrays). So array_foreach inside a script loops over the caller's array, whatever its size, and it can be given any of your scripts to run.
One call of your script for each place the array has, and nothing else. No memory is set aside.
var positions = array_create(3, 0);
positions[0] = 20;
positions[1] = 60;
positions[2] = 100;
array_foreach(positions, spawn_coin); // three coins, at x 20, 60 and 100A plain loop over the array_length(target) places. The script is called once for each item, from the first place to the last, as an ordinary C++ call (`game::name(item)`) inside a loop that reads the length again every time round, so a script that changed the container it was walking could not make the loop read past the end. What the script gives back is ignored. The script is the name of one of your own scripts, or a function written out in the middle of the line (function (n) { ... }), which is written out as a C++ function where it stands and can see the variables around it; either way it must take exactly one argument whose type accepts the items (a function with none written takes the items' kind). It costs one call of the script per item and nothing else: no memory is set aside.